package com.ivt.model.view;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;

public class QueryCriteria implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String criteria;

	private Integer pageSize;

	private Map<String, Object> mapCriteria;

	private Integer page;

	private int offset;

	public int getOffset() {
		if (null == page || 0 >= page) {
			offset = 0;
		} else {
			offset = pageSize * (page - 1);
		}
		return offset;
	}

	public void setCriteria(String criteria) {
		this.criteria = criteria;
	}

	public Integer getPageSize() {

		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = (null == pageSize || 0 >= pageSize) ? 30 : pageSize;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	@Override
	public String toString() {
		return "QueryMap [criteria=" + criteria + ", pageSize=" + pageSize
				+ ", page=" + page + "]";
	}

	public Map<String, Object> splitCriteria() {
		if (StringUtils.isNotBlank(criteria)) {
			JSONObject o = JSONObject.parseObject(criteria);
			mapCriteria = Maps.newHashMap();
			Set<String> keys = o.keySet();

			for (String key : keys) {
				if (StringUtils.isNotBlank(o.getString(key))) {
					mapCriteria.put(key, o.getString(key));
				}
			}
		}
		return mapCriteria;
	}

	public Map<String, Object> findCriteria() {
		return mapCriteria;
	}

}
